package com.jcl.recruit.console.api.db;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.db.utils.DataSourceUtils;
import com.jcl.core.resp.ApiResponse;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.console.db.service.DbServerService;
import com.jcl.recruit.console.vo.ConsoleDbServerVo;
import com.jcl.recruit.core.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName DbServerController
 * @Author yyliu
 * @Date 2018/12/21 14:31
 * @Version 1.0
 **/
@RestController
@RequestMapping("/console/db")
public class DbServerController {

    @Autowired
    private DbServerService dbServerService;

    @GetMapping("/pageList")
    public ApiResponse pageList(Integer curPage, Integer rowSize) {
        PageResult pageResult = dbServerService.pageList(curPage, rowSize);
        return ApiResponse.success(pageResult);
    }

    @GetMapping("/get/{id}")
    public ApiResponse pageList(@PathVariable("id") Integer id) {
        ConsoleDbServerVo vo = dbServerService.getOne(id);
        if (null == vo) {
            return ApiResponse.error(Constants.ReturnInfo.ERROR_RETURN_NULL_MSG);
        }
        return ApiResponse.success(vo);
    }

    @PostMapping("/add")
    public ApiResponse add(@RequestBody ConsoleDbServerVo serverVo) {
        if (StringUtils.isEmpty(serverVo.getServerName())) {
            return ApiResponse.error("当前数据库名称不能为空！");
        }
        if (StringUtils.isEmpty(serverVo.getDbIp())
                || StringUtils.isEmpty(serverVo.getDbPort())
                || StringUtils.isEmpty(serverVo.getDbUsername())
                || StringUtils.isEmpty(serverVo.getDbPassword())
                || StringUtils.isEmpty(serverVo.getDbName())) {
            return ApiResponse.error("当前数据库连接信息不完整！");
        }
        if (null != serverVo.getCorpNumber() && serverVo.getCorpNumber() < 0) {
            return ApiResponse.error("增添的企业数量不能为负数！");
        }
        String url = DataSourceUtils.makeMysqlDriverUrl(serverVo.getDbIp(), serverVo.getDbPort(),
                serverVo.getDbUrlParam(), serverVo.getDbName());
        boolean isOk = DataSourceUtils.testDbConnect(DataSourceUtils.DRIVER_CLASS_NAME,
                url, serverVo.getDbUsername(), serverVo.getDbPassword());
        if (!isOk) {
            return ApiResponse.error("数据库连接不上，请确认地址，账号，密码！");
        }
        serverVo.setDbUrl(url);
        int num = dbServerService.addConsoleDbServer(serverVo);
        if (num == 1) {
            return ApiResponse.error("当前数据库唯一标识已经存在，不能新增");
        }
        return ApiResponse.success(Constants.ReturnInfo.SUCCESS_MSG);
    }

    @GetMapping("/prefix/count")
    public ApiResponse prefixCount() {
        Map<String, Integer> dataMap = dbServerService.prefixMap();
        return ApiResponse.success(dataMap);
    }

    @PostMapping("/append")
    public ApiResponse update(@RequestBody ConsoleDbServerVo serverVo) {
        if (null == serverVo.getId()) {
            return ApiResponse.error(Constants.ReturnInfo.ERROR_PARAM_NULL_MSG);
        }
        int num = dbServerService.updateConsoleDbServer(serverVo.getId(), serverVo.getCorpNumber());
        if (num == 0) {
            return ApiResponse.error(Constants.ReturnInfo.ERROR_RETURN_NULL_MSG);
        }
        return ApiResponse.success(Constants.ReturnInfo.SUCCESS_MSG);
    }

}
